package zrar.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

public class PropertiesUtil {

	// 读取properties的全部信息
	public static Map<String,String> readProperties(String filePath) {
		
		Map<String,String> propsMap = new HashMap<String,String>();
		
		Properties props = new Properties();
		try {
			//InputStream ini = new BufferedInputStream(new FileInputStream(filePath));
			InputStreamReader in=new InputStreamReader(new FileInputStream(filePath),"UTF-8");
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String Property = props.getProperty(key);
				propsMap.put(key, Property);
				//System.out.println(key +"="+ Property);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return propsMap;
	}

	// 写入properties信息
	public static void writeProperties(String filePath,Map<String,String> inputData) {
		Properties prop = new Properties();
		try {
			//InputStream fis = new FileInputStream(filePath);
			InputStreamReader in=new InputStreamReader(new FileInputStream("award.properties"),"UTF-8");									
			prop.load(in);
									
			//OutputStream fos = new FileOutputStream(filePath);
			OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8");
			
			for(Entry<String, String> entry : inputData.entrySet()){
				//System.out.println("key:"+entry.getKey()+","+"value:"+entry.getValue());	
				prop.setProperty(entry.getKey(), entry.getValue());
			}
			
			prop.store(writer,null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
